<template>
  <div class="detail-conpotin">
    <el-form ref="ruleFormsdfs" :model="userInfo" :rules="rules" label-width="200px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="userInfo.username" style="width:300px" size="mini" />
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="userInfo.workNumber" style="width:300px" :disabled="isEdit" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="userInfo.mobile" style="width:300px" :disabled="isEdit" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentId">
        <!-- 部门子组件 -->
        <!-- <departmenComVue v-model="userInfo.departmentId" /> -->
        <departmenComVue :department-id.sync="userInfo.departmentId" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="userInfo.formOfEmployment" placeholder="请选择" clearable>
          <el-option
            label="正式"
            :value="1"
          />
          <el-option
            label="非正式"
            :value="2"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          format="yyyy年-MM月-dd日"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="userInfo.correctionTime"
          type="date"
          format="yyyy年-MM月-dd日"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="员工头像">
        <!-- 子组件 -->
        <uploadImgVue v-model="userInfo.staffPhoto" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 导入组件
import departmenComVue from './components/departmenCom.vue'
import uploadImgVue from './components/uploadImg.vue'
// 导入新增接口
import { getUserRequest, getSuerRequest, PutSuerRequest } from '@/api/employee.js'
export default {
  name: 'Detail',
  components: {
    departmenComVue,
    uploadImgVue
  },
  data() {
    const validateCorrectionTime = (rule, value, callback) => {
      if (+new Date(this.userInfo.timeOfEntry) > +new Date(value)) {
        callback(new Error('转正时间不能早于入职时间'))
        return
      }
      callback()
    }
    return {
      // 表单数据
      userInfo: {
        username: '',
        workNumber: '',
        mobile: '',
        departmentId: 1,
        formOfEmployment: 1,
        timeOfEntry: '',
        correctionTime: '',
        staffPhoto: ''
      },
      // 校验
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名长度为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^1[3|5|6|7|8|9]\d{9}$/, message: '手机号不合法', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'change' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'change' },
          { 'validator': validateCorrectionTime, 'trigger': 'change' }
        ]
      }
    }
  },
  computed: {
    isEdit() {
      return !!this.$route.params.id
    }
  },
  created() {
    if (this.$route.params.id) {
      this.getSuer()
    }
  },
  methods: {
    // 点击确定,进行新增
    submitForm() {
      this.$refs.ruleFormsdfs.validate(async valid => {
        if (valid) {
          try {
            if (this.isEdit) {
              await PutSuerRequest(this.userInfo)
            } else {
              await getUserRequest(this.userInfo)
            }
            this.$router.back()
          } catch (err) {
            console.log('更新失败')
          }
        } else {
          console.log('有某一个校验失败')
        }
      })
    },
    // 数据回显
    async getSuer() {
      const data = await getSuerRequest(this.$route.params.id)
      this.userInfo = data.data
    }
  }
}
</script>

<style lang="scss" scoped>
.detail-conpotin{
   padding: 20px;
  }
  ::v-deep .el-input{
    width:300px
  }
</style>
<style>
.detail-conpotin .el-input__inner {
    height: 28px;
    line-height: 28px;
}
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
